//! @Author: DengLibin
//! @Date: Create in 2025-01-20 14:55:36
//! @Description:

use std::vec;

use rocketmq::conf::{ClientOption, ProducerOption};
use rocketmq::model::message::MessageBuilder;
use rocketmq::Producer;

pub async fn demo_send() {
    // recommend to specify which topic(s) you would like to send message to
    // producer will prefetch topic route when start and failed fast if topic not exist
    let mut producer_option = ProducerOption::default();
    producer_option.set_topics(vec!["deng"]);

    // set which rocketmq proxy to connect
    let mut client_option = ClientOption::default();
    //默认情况下，Proxy 的 8080 端口提供 Remoting 协议的接入，8081 端口提供 gRPC 协议的接入
    client_option.set_access_url("localhost:8081");

    // build and start producer
    let mut producer = Producer::new(producer_option, client_option).unwrap();
    producer.start().await.unwrap();

    // build message
    let message = MessageBuilder::builder()
        .set_topic("deng")
        .set_tag("tag")
        .set_keys(vec!["key1", "key2"])
        .set_body("hello world, 来自生产者的消息".as_bytes().to_vec())
        .build()
        .unwrap();

    // send message to rocketmq proxy
    let result = producer.send(message).await;
    debug_assert!(result.is_ok(), "send message failed: {:?}", result);
    println!(
        "send message success, message_id={}",
        result.unwrap().message_id()
    );
}
